package com.solution.service;

import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.ClientErrorMessages;
import com.solution.constant.ErrorMessages;
import com.solution.constant.StatusConstants;
import com.solution.dao.ClientTypeDAO;
import com.solution.dao.DAOFactory;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.ClientType;
import com.solution.util.SearchVO;

@Stateless
@Remote(ClientTypeService.class)
@RemoteBinding(jndiBinding="ClientTypeService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ClientTypeServiceImpl implements ClientTypeService{

	/**
	 * DAO instance.
	 */
	private ClientTypeDAO clientTypeDao = (ClientTypeDAO)DAOFactory.getDAOInstance(ClientTypeDAO.class.getName());
	
	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(ClientTypeServiceImpl.class.getName());
	
	public ClientType createClient(ClientType clientType) throws ServiceBeanException{
		log.debug("Entering :: ClientTypeServiceImpl:createClient()");
		try{
			// Perform business validations.
			// check for the mandatory parameters.
			if(clientType.checkForMandatoryParameters() != null && clientType.checkForMandatoryParameters().size() > 0){
				String errorMsg = ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION;
				Iterator<String> iter = clientType.checkForMandatoryParameters().iterator();
				while(iter.hasNext()){
					errorMsg = errorMsg+", "+iter.next();
				}
				throw new ServiceBeanException(errorMsg);
			}
			else{
				if(clientTypeDao.verifyUnique("clientName", clientType.getClientName(), null)){
					// Create the answer in database.
					clientType = clientTypeDao.create(clientType);
					log.info("The new client created with ID : "+clientType.getId().longValue());
					log.debug("Exiting :: ClientTypeServiceImpl:createClient()");
					return clientType;
				}
				else{
					throw new ServiceBeanException(ClientErrorMessages.CLIENT_UNIQUE_NAME_EXCEPTION);
				}
			}
		}
		catch(DAOException ex){
			log.error(ClientErrorMessages.CLIENT_CREATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public ClientType modifyClient(ClientType clientType) throws ServiceBeanException{
		log.debug("Entering :: ClientTypeServiceImpl:modifyClient()");
		try{
			// Perform business validations.
			// check for the mandatory parameters.
			if(clientType.checkForMandatoryParameters() != null && clientType.checkForMandatoryParameters().size() > 0){
				String errorMsg = ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION;
				Iterator<String> iter = clientType.checkForMandatoryParameters().iterator();
				while(iter.hasNext()){
					errorMsg = errorMsg+", "+iter.next();
				}
				throw new ServiceBeanException(errorMsg);
			}
			else{
				if(clientTypeDao.verifyUnique("clientName", clientType.getClientName(), clientType.getId())){
					// Create the answer in database.
					clientType = clientTypeDao.update(clientType);
					log.info("The client updated with ID : "+clientType.getId().longValue());
					log.debug("Exiting :: ClientTypeServiceImpl:modifyClient()");
					return clientType;
				}
				else{
					throw new ServiceBeanException(ClientErrorMessages.CLIENT_UNIQUE_NAME_EXCEPTION);
				}
			}
		}
		catch(DAOException ex){
			log.error(ClientErrorMessages.CLIENT_MODIFY_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public List<ClientType> fetchActiveClients() throws ServiceBeanException{
		log.debug("Entering :: ClientTypeServiceImpl:fetchActiveClients()");
		try{
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			return clientTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ClientErrorMessages.CLIENT_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public ClientType getClient(Long clientTypeId) throws ServiceBeanException{
		log.debug("Entering :: ClientTypeServiceImpl:getClient()");
		try{
			if(clientTypeId != null){
				SearchVO searchVO = new SearchVO();
				searchVO.addEqualsCondition("clientType.id", clientTypeId, true);
				return clientTypeDao.searchUnique(searchVO);
			}
			else{
				throw new ServiceBeanException(ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION+" clientTypeId");
			}
		}
		catch(DAOException ex){
			log.error(ClientErrorMessages.CLIENT_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public List<ClientType> getClients(SearchVO searchVO) throws ServiceBeanException{
		log.debug("Entering :: ClientTypeServiceImpl:getClients()");
		try{
			return clientTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ClientErrorMessages.CLIENT_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
}
